package com.landi.logger.utils;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.android.LogcatAppender;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;

/**
 * @author chenxumeng
 * @description
 */
public class LogcatUtil {
    private LogcatAppender logcatAppender;
    private LoggerContext context = new LoggerContext();
    private Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME);
    private Logger logcat = null;
    public void init(){
        context.reset();
        root.detachAndStopAllAppenders();
        configureLogcatAppender();
    }
    private void configureLogcatAppender() {
        logcatAppender = new LogcatAppender();
        logcatAppender.setContext(context);
        logcatAppender.setName("LOGCAT");

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(context);
        encoder.setPattern("%msg%n");
        encoder.start();

        PatternLayoutEncoder tagEncoder = new PatternLayoutEncoder();
        tagEncoder.setContext(context);
        tagEncoder.setPattern("MainActivity");
        tagEncoder.start();

       // logcatAppender.setTagEncoder(tagEncoder);
        logcatAppender.setEncoder(encoder);
        logcatAppender.start();

        root.addAppender(logcatAppender);
    }
    public void info(Class clazz,String info){
        context.getLogger(clazz).info(info);
        context.getLogger("LOGCAT").info(info);
    }
}
